/*
 * This file is part of LibrePlan
 *
 * Copyright (C) 2013 St. Antoniusziekenhuis
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.libreplan.web.logs;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.libreplan.business.common.IntegrationEntity;
import org.libreplan.business.common.daos.IConfigurationDAO;
import org.libreplan.business.common.entities.EntityNameEnum;
import org.libreplan.business.common.exceptions.InstanceNotFoundException;
import org.libreplan.business.common.exceptions.ValidationException;
import org.libreplan.business.logs.daos.IIssueLogDAO;
import org.libreplan.business.logs.daos.IProjectLogDAO;
import org.libreplan.business.logs.entities.IssueLog;
import org.libreplan.business.orders.daos.IOrderDAO;
import org.libreplan.business.orders.entities.Order;
import org.libreplan.business.users.daos.IUserDAO;
import org.libreplan.business.users.entities.User;
import org.libreplan.web.common.IntegrationEntityModel;
import org.libreplan.web.common.concurrentdetection.OnConcurrentModification;
import org.libreplan.web.security.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * Model for UI operations related to {@link IssueLog}.
 *
 * @author Misha Gozhda <misha@libreplan-enterprise.com>
 */
@Service
@Scope(BeanDefinition.SCOPE_PROTOTYPE)
@OnConcurrentModification(goToPage = "/logs/_logs.zul")
public class IssueLogModel extends IntegrationEntityModel implements IIssueLogModel {

    private IssueLog issueLog;

    @Autowired
    private IIssueLogDAO issueLogDAO;

    @Autowired
    private IProjectLogDAO projectLogDAO;

    @Autowired
    private IUserDAO userDAO;

    @Autowired
    private IOrderDAO orderDAO;

    @Autowired
    private IConfigurationDAO configurationDAO;

    @Override
    @Transactional(readOnly = true)
    public List<IssueLog> getIssueLogs() {
        return projectLogDAO.getIssueLogs();
    }

    @Override
    @Transactional(readOnly = true)
    public List<Order> getOrders() {
        return orderDAO.getOrders();
    }

    @Override
    @Transactional(readOnly = true)
    public List<User> getUsers() {
        List<User> users = new ArrayList<>();
        users.addAll(userDAO.findAll());
        return users;
    }

    @Override
    @Transactional(readOnly = true)
    public void initCreate() {
        boolean codeGenerated = configurationDAO.getConfiguration().getGenerateCodeForProjectLog();
        this.issueLog = IssueLog.create();
        if (codeGenerated) {
            issueLog.setCodeAutogenerated(codeGenerated);
            setDefaultCode();
            try {
                issueLog.setCreatedBy(userDAO.findByLoginName(SecurityUtils.getSessionUserLoginName()));
            } catch (InstanceNotFoundException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void initEdit(IssueLog issueLog) {
        this.issueLog = issueLog;
    }

    @Override
    public IssueLog getIssueLog() {
        return this.issueLog;
    }

    @Override
    @Transactional
    public void confirmSave() throws ValidationException {
        issueLogDAO.save(issueLog);
    }

    @Override
    public void cancel() {
        issueLog = null;
    }

    @Override
    @Transactional
    public void remove(IssueLog issueLog) {
        try {
            issueLogDAO.remove(issueLog.getId());
        } catch (InstanceNotFoundException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public List<IssueLog> getByParent(Order order) {
        return issueLogDAO.getByParent(order);
    }

    @Override
    public void setIssueLog(IssueLog log) {
        this.issueLog = log;
    }

    @Override
    public EntityNameEnum getEntityName() {
        return EntityNameEnum.ISSUE_LOG;
    }

    @Override
    public IntegrationEntity getCurrentEntity() {
        return this.issueLog;
    }

    @Override
    protected Set<IntegrationEntity> getChildren() {
        return new HashSet<>();
    }

    @Override
    public Order getOrder() {
        return issueLog.getOrder();
    }

    @Override
    public void setOrder(Order order) {
        if (this.issueLog != null) {
            IssueLog issueLog = getIssueLog();
            issueLog.setOrder(order);
        }
    }

    @Override
    public User getCreatedBy() {
        return issueLog.getCreatedBy();
    }

    @Override
    public void setCreatedBy(User user) {
        if (this.issueLog != null) {
            IssueLog issueLog = getIssueLog();
            issueLog.setCreatedBy(user);
        }
    }

}
